package com.yun2.bms.controller.lend;

import com.yun2.bms.model.Book;
import com.yun2.bms.model.Lend;
import com.yun2.bms.model.User;
import com.yun2.bms.service.ILendService;
import com.yun2.bms.service.IUserService;
import com.yun2.bms.service.impl.LendServiceImpl;
import com.yun2.bms.service.impl.UserServiceImpl;
import com.yun2.bms.utils.StringUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/6/18
 */

public class ReturnController implements Initializable {

    private IUserService userService = new UserServiceImpl();

    private ILendService lendService = new LendServiceImpl();

    private ObservableList<Lend> lendData = FXCollections.observableArrayList();

    @FXML
    private TableColumn<Lend, Lend> expireDays;

    @FXML
    private Label lblUserInfo;

    @FXML
    private Button btnQuery;

    @FXML
    private TableView<Lend> lendTable;

    @FXML
    private TableColumn<Lend, Lend> userName;

    @FXML
    private TableColumn<Lend, Lend> userId;

    @FXML
    private TableColumn<Lend, Lend> bookName;

    @FXML
    private TableColumn<Lend, Lend> bookId;

    @FXML
    private TableColumn<Lend, Lend> returnDate;

    @FXML
    private Pagination pager;

    @FXML
    private Button btnAdd;

    @FXML
    private TextField tfUserInfo;

    @FXML
    private TableColumn<Lend, Lend> lendDate;

    @FXML
    private TableColumn<Lend, Lend> id;

    @FXML
    private Button btnReset;

    @FXML
    void add(ActionEvent event) {

    }

    @FXML
    void query(ActionEvent event) {
        //1 获取用户的信息
        String id = this.tfUserInfo.getText();
        if (StringUtils.isNotBlank(id)) {
            //2 查询用户的信息
            User currentUser = this.userService.getUser4Lend(Integer.valueOf(id));
            updateUserInfo(currentUser);
            if(currentUser != null){
                //3 显示该用户所有的借阅记录
                List<Lend> lendList = this.lendService.getAllByUserId(currentUser.getId());
                lendData.clear();
                lendData.addAll(lendList);
                this.lendTable.setItems(lendData);
            }
        }
    }

    private void updateUserInfo(User currentUser){
        StringBuilder info = new StringBuilder();
        if(currentUser == null){
            info.append("用户不存在！");
        }else{
            info.append("用户编号：").append(currentUser.getId()).append(",");
            info.append("用户姓名：").append(currentUser.getUserName()).append(",");
            info.append("用户角色：").append(currentUser.getRoleCn()).append(",");
            info.append("最大借阅数量：").append(currentUser.getMaxLendNumber()).append(",");
            //下面的信息必须去lend表中去获取
            info.append("已经借阅数量：").append(currentUser.getMyLendedNumber()).append("。");
        }
        this.lblUserInfo.setText(info.toString());
    }

    @FXML
    void reset(ActionEvent event) {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
